package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.soft.idc.common.model.IdcAgent;
import com.soft.idc.common.util.JQPage;

public class AgentManagerController extends Controller {
	public void index() {
		this.render("/perf_kpi_manager/agent.html");
	}
	
	public void queryAgent() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		StringBuffer sb = new StringBuffer();
		String code = this.getPara("code");
		String name = this.getPara("name");
		sb.append("select a.agent_id, a.code, a.name, a.ip, a.port ");
		
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append("from idc_agent a  where 1=1 ");
		
		if(code !=null ){
			sqlFrom.append(" and a.code like '%" + code + "%' ");
		}
		
		if(name !=null ){
			sqlFrom.append(" and a.name like '%" + name + "%' ");
		}
		
		int pageNum = start/length+1;
		
		Page<Record> page = Db.paginate(pageNum, length, sb.toString(), sqlFrom.toString());
		JQPage jPage = new JQPage(page);
		System.out.println(JsonKit.toJson(jPage));
		this.renderJson(jPage);
		
	}
	
	public void add() {
		String code = this.getPara("code");
		String name = this.getPara("name");
		String ip = this.getPara("ip");
		String port = this.getPara("port");
		IdcAgent agent = new IdcAgent();
		agent.setCode(code);
		agent.setName(name);
		agent.setIp(ip);
		agent.setPort(port);
		boolean result = agent.save();
		this.renderJson(result);
	}
	
	public void delete() {
		final String agentIds = this.getPara("agentIds");
		ICallback call =new ICallback() {
			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] agentIdArr = agentIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("delete from idc_agent where agent_id in(");
				for(int i=0; i<agentIdArr.length; i++){
					if(i==agentIdArr.length-1){
						sb.append(agentIdArr[i] + ")");
					}else{
						sb.append(agentIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count=sta.executeUpdate(sb.toString());
				if(count==agentIdArr.length){
					conn.commit();
					conn.close();
					return true;
				}else{
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}
	
	public void edit() {
		String agent_id = this.getPara("agent_id");
		String code = this.getPara("code");
		String name = this.getPara("name");
		String ip = this.getPara("ip");
		String port = this.getPara("port");
		IdcAgent agent = new IdcAgent();
		agent = agent.findById(Integer.parseInt(agent_id));
		agent.setCode(code);
		agent.setName(name);
		agent.setIp(ip);
		agent.setPort(port);
		boolean result = agent.update();
		this.renderJson(result);
	}
}
